// 发送消息到 iframe
export const postMessageToIframe = (message: string) => {
  const iframe = querySelector("#iframeId");
  iframe.contentWindow.postMessage(message, "*");
};

// 接收子页面发来的消息
export const receiveMessageFromeIframe = () => {};
window.addEventListener(
  "message",
  function (event) {
    // 验证消息来源
    // if (event.origin !== import.meta.env.VITE_H5) return;
    console.log("收到来自iframe的消息:", event.data);
  },
  false
);

export function calculateAge(birthdate) {
  birthdate = new Date(birthdate);
  const today = new Date();
  const birthYear = birthdate.getFullYear();

  if (today.getMonth() < birthdate.getMonth()) {
    return today.getFullYear() - birthYear;
  } else if (
    today.getMonth() === birthdate.getMonth() &&
    today.getDay() < birthdate.getDay()
  ) {
    return today.getFullYear() - birthYear - 1;
  } else {
    return today.getFullYear() - birthYear;
  }
}
